package com.zxm;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {
        List<String> images = Arrays.asList("images\\b12.png",
                "images\\f13.png","images\\s9.png","images\\s11.png");
        Collections.shuffle(images);

        JFrame jFrame = new JFrame("拱猪");

        JPanel jPanel = new JPanel();
        jPanel.setLayout(null);
        jPanel.setBackground(Color.GRAY);

        JButton jButton = new JButton("出牌");
        jPanel.add(jButton);
        jButton.setBounds(450, 100, 100, 40);

        Font font = new Font("宋体", Font.BOLD, 20);

        JLabel jl1 = new JLabel("玉环");
        jl1.setFont(font);
        jl1.setForeground(Color.white);
        jl1.setBounds(475, 60, 100, 40);
        jl1.requestFocus();
        jPanel.add(jl1);

        JLabel jl2 = new JLabel("西施");
        jl2.setFont(font);
        jl2.setForeground(Color.white);
        jl2.setBounds(900, 500, 100, 40);
        jPanel.add(jl2);

        JLabel jl3 = new JLabel("貂蝉");
        jl3.setFont(font);
        jl3.setForeground(Color.white);
        jl3.setBounds(475, 900, 100, 40);
        jPanel.add(jl3);

        JLabel jl4 = new JLabel("昭君");
        jl4.setFont(font);
        jl4.setForeground(Color.white);
        jl4.setBounds(50, 500, 100, 40);
        jPanel.add(jl4);

        jFrame.add(jPanel);
        jFrame.setSize(1000 , 1000);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        jFrame.setVisible(true);

        //创建责任链
        YuHuan yuHuan = new YuHuan();
        XiShi xiShi = new XiShi();
        DiaoChan diaoChan = new DiaoChan();
        ZhaoJun zhaoJun = new ZhaoJun();
        yuHuan.setNextHandler(xiShi);
        xiShi.setNextHandler(diaoChan);
        diaoChan.setNextHandler(zhaoJun);

        //弹窗
        int i = 0;
        for (String image : images) {
            if (image == "images\\s11.png"){
                break;
            }
            i++;
        }
        String res = yuHuan.handlerRequest(i);
        jButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Image image1 = null;
                try {
                    image1 = ImageIO.read(new File(images.get(0)));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                Image image2 = null;
                try {
                    image2 = ImageIO.read(new File(images.get(1)));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                Image image3 = null;
                try {
                    image3 = ImageIO.read(new File(images.get(2)));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                Image image4 = null;
                try {
                    image4 = ImageIO.read(new File(images.get(3)));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                jPanel.getGraphics().drawImage(image1, 475, 200, null);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                jPanel.getGraphics().drawImage(image2, 800, 500, null);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                jPanel.getGraphics().drawImage(image3, 475, 800, null);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                jPanel.getGraphics().drawImage(image4, 200, 500, null);
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
                JOptionPane.showMessageDialog(null, res,"消息", JOptionPane.INFORMATION_MESSAGE);
            }
        });

    }
}
